package com.example.ihs.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ihs.common.result.Result;
import com.example.ihs.entity.Family;
import com.example.ihs.service.FamilyService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 家庭信息表 前端控制器
 * </p>
 *
 * @author universe
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/ihs/family")
public class FamilyController {
    @Autowired
    private FamilyService service;
    //条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        Family family) {
        //创建page对象
        Page<Family> pageParam = new Page<>(page,limit);
        //封装条件，判断条件值不为空
        LambdaQueryWrapper<Family> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String familyMessage = family.getFamilyMessage();
        //判断条件值不为空
        //like 模糊查询
        if(!StringUtils.isEmpty(familyMessage)) {
            wrapper.like(Family::getFamilyMessage,familyMessage);
        }
        //调用mp的方法实现条件分页查询
        IPage<Family> pageModel = service.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Family family = service.getById(id);
        return Result.ok(family);
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    public Result save(@RequestBody Family family) {

        service.save(family);
        return Result.ok();
    }

    @ApiOperation(value = "更新")
    @PutMapping("update")
    public Result updateById(@RequestBody Family family) {
        service.updateById(family);
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "获取所有")
    @GetMapping("getAll")
    public Result getAll() {
        List<Family> list = service.list();
        return Result.ok(list);
    }
}

